package tec.line;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class PanelDomain {
	
	int index = 1;
	int real_width;//实体宽度
	int black_width;//空白宽度
	int px_width;//panel所占横向像素值
	int px_hight;//panel所占纵向像素值
	int index_max_real;//指数最大值
	int index_min_real;//指数最小值
	int index_max_show;//指数最大显示值
	int index_min_show;//指数最小显示值
	int index_domain;//所显示的指数域绝对值(index_max_show - index_min_show)
	int left_fix;//左边距
	List<IndexData> datas;//数据实体
	List<IndexData> show_datas;//数据实体
	List<IndexData> vertical_Line;//数据实体
	Executor executor = Executors.newFixedThreadPool(5);
	
	public List<IndexData> getShow_datas() {
		return show_datas;
	}
	public void setShow_datas(List<IndexData> show_datas) {
//		Random random = new Random();
      int start = index++;
      if (start >30) {
		start = 30;
	}
//      while (index < 10) {
//    	  start = random.nextInt(30);
//	}
      int length = (this.getPx_width() - this.left_fix)/(this.getBlack_width() + this.getReal_width());
      
      List<IndexData> dataList = new ArrayList<IndexData>();
      
      dataList.addAll(show_datas.subList(start-1, start + length -1));
		
      
      System.out.println(start-1 +";" + (start + length -1));
		
		this.show_datas = dataList;
		this.vertical_Line = new ArrayList<IndexData>();
	}
	public int getReal_width() {
		return real_width;
	}
	public void setReal_width(int real_width) {
		this.real_width = real_width;
	}
	public int getBlack_width() {
		return black_width;
	}
	public List<IndexData> getVertical_Line() {
		return vertical_Line;
	}
	public void setVertical_Line(List<IndexData> vertical_Line) {
		this.vertical_Line = vertical_Line;
	}
	public void setBlack_width(int black_width) {
		this.black_width = black_width;
	}
	public int getPx_width() {
		return px_width;
	}
	public void setPx_width(int px_width) {
		this.px_width = px_width;
	}
	public int getPx_hight() {
		return px_hight;
	}
	public void setPx_hight(int px_hight) {
		this.px_hight = px_hight;
	}
	public int getIndex_max_real() {
		return index_max_real;
	}
	public void setIndex_max_real(int index_max_real) {
		this.index_max_real = index_max_real;
	}
	public int getIndex_min_real() {
		return index_min_real;
	}
	public void setIndex_min_real(int index_min_real) {
		this.index_min_real = index_min_real;
	}
	public int getIndex_max_show() {
		return index_max_show;
	}
	public void setIndex_max_show(int index_max_show) {
		this.index_max_show = index_max_show;
	}
	public int getIndex_min_show() {
		return index_min_show;
	}
	public void setIndex_min_show(int index_min_show) {
		this.index_min_show = index_min_show;
	}
	public int getIndex_domain() {
		return index_domain;
	}
	public void setIndex_domain(int index_domain) {
		this.index_domain = index_domain;
	}
	public int getLeft_fix() {
		return left_fix;
	}
	public void setLeft_fix(int left_fix) {
		this.left_fix = left_fix;
	}
	public List<IndexData> getDatas() {
		return datas;
	}
	public void setDatas(List<IndexData> datas) {
		this.datas = datas;
	}
	
	
	public void calculate(final PanelDomain pd) {
		final CountDownLatch latch = new CountDownLatch(1);
		final CountDownLatch latch_end = new CountDownLatch(2);
		
		Runnable run = new Runnable() {
			
			public void run() {
				int index_max_real = 0;//指数最大值
				int index_min_real = 0;//指数最小值
				
				List<IndexData> list = pd.getShow_datas();
				for (int i = 0; i < list.size(); i++) {
					if (index_max_real < list.get(i).getIndex_max()) 
						index_max_real = list.get(i).getIndex_max();
					if (index_min_real > list.get(i).getIndex_min() || index_min_real == 0)
						index_min_real = list.get(i).getIndex_min();
				}
				
				pd.setIndex_max_real(index_max_real);
				pd.setIndex_min_real(index_min_real);
				pd.setIndex_max_show((50 - (index_max_real%50)) + index_max_real);
				pd.setIndex_min_show(index_min_real - (50 - (index_min_real%50)));
				pd.setIndex_domain(pd.getIndex_max_show() - pd.getIndex_min_show());
				latch.countDown();
			}
		};
		
		executor.execute(run);
		
		Runnable run_datas = new Runnable() {
			
			List<IndexData> list = pd.getShow_datas();
			public void run() {
				try {
					latch.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				for (int i = 0; i < list.size(); i++) {
					IndexData idata = list.get(i);
					idata.getP_max().setX(pd.getLeft_fix() + i * (pd.getBlack_width() + pd.getReal_width()) + pd.getReal_width()/2 -1);
					idata.getP_max().setY((int)(((float)idata.getIndex_max() - pd.getIndex_min_show())*pd.getPx_hight()/pd.getIndex_domain()));
					
					idata.getP_min().setX(pd.getLeft_fix() + i * (pd.getBlack_width() + pd.getReal_width()) + pd.getReal_width()/2 -1);
					idata.getP_min().setY((int)(((float)idata.getIndex_min() - pd.getIndex_min_show())*pd.getPx_hight()/pd.getIndex_domain()));
					
					idata.getP_open().setX(pd.getLeft_fix() + i * (pd.getBlack_width() + pd.getReal_width()));;
					idata.getP_open().setY((int)(((float)idata.getIndex_open() - pd.getIndex_min_show())*pd.getPx_hight()/pd.getIndex_domain()));
					idata.getP_close().setX(pd.getLeft_fix() + i * (pd.getBlack_width() + pd.getReal_width()));
					idata.getP_close().setY((int)(((float)idata.getIndex_close() - pd.getIndex_min_show())*pd.getPx_hight()/pd.getIndex_domain()));
				}
				
				latch_end.countDown();
			}
		};
		executor.execute(run_datas);
		
		Runnable run_verticalLine = new Runnable() {
			
			List<IndexData> list = pd.getShow_datas();
			
			List<IndexData> vertical_List = pd.getVertical_Line() ;
			public void run() {
				try {
					latch.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
//				System.out.println("size:" + list.size());
				for (int i = 0; i < list.size(); i++) {
					IndexData idata = list.get(i);
//					System.out.println("i:" + i +";idata:" + idata);
					if (idata.getId()%10 == 0) {
						
						IndexData ida = new IndexData();
						ida.getP_open().setX(pd.getLeft_fix() + pd.getReal_width()/2 + i * (pd.getBlack_width() + pd.getReal_width()));
						System.out.println("x:" + (pd.getLeft_fix() + i * (pd.getBlack_width() + pd.getReal_width()) + pd.getReal_width()/2));
						vertical_List.add(ida);
					}
				}
				
				latch_end.countDown();
			}
		};
		executor.execute(run_verticalLine);
		
		try {
			latch.await();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
